package com.wholeintech.zigbee.util;


import android.content.Context;
import android.util.Log;

import com.wholeintech.entity.DeviceDb;
import com.wholeintech.utils.CommonUtil;
import com.wholeintech.zigbee.gw.DeviceManager;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 *
 */
public class DeviceDbManager {
    private String TAG = "DeviceDbManager";
    private static DeviceDbManager mDeviceDbManager;

    private static Context mContext;

    private DeviceDbManager(Context Context) {
        mContext = Context;
    }

    public static DeviceDbManager getInstance(Context Context) {

        if (mDeviceDbManager == null) {
            mDeviceDbManager = new DeviceDbManager(Context);
        }
        return mDeviceDbManager;
    }

    public int add(String boxId, int network, int type, final String gwDevId,
                   final String devId, String mac, String name, String bindSw1,
                   String bindSw2, int regionId, String devCfg) {
        DbManager db = CommonUtil.MyDbUtils();
        int dimmer = 0;
        try {
            Log.d(TAG, "addNewDev===regionId:" + regionId);

            DeviceDb ZBData = new DeviceDb(boxId, gwDevId, devId, name,
                    network, type, mac, bindSw1, bindSw2, regionId, devCfg);
            DeviceDb me = db.selector(DeviceDb.class).where("devId", "=", devId).findFirst();

            if (type == DeviceManager.TYPE_LIGHT_DIMMER) // raymanfeng
            {
                byte[] byteDevId = devId.getBytes();
                if (byteDevId[4] == '0')
                    dimmer = 0;
                else
                    dimmer = 1;
            }

            if (me == null) {

                ZBData.setDimmer(dimmer);
                ZBData.setName(name);
                db.save(ZBData);
            } else {
                int bindupd = 0;
                if (me.getBindSw1() != null) {
                    if (!me.getBindSw1().equals(bindSw1)) {
                        bindupd = 1;
                    }
                }
                if (me.getBindSw2() != null) {
                    if (!me.getBindSw2().equals(bindSw2)) {
                        bindupd = 1;
                    }
                }

                if (me.getDevCfg() != null) {
                    if (!me.getDevCfg().equals(devCfg)) {
                        bindupd = 1;
                    }
                }

                if (!me.getDevId().equals(devId)
                        || me.getType() != type || me.getRegionId() != regionId
                        || !me.getName().equals(name) || bindupd == 1) {
                    me.setDevId(devId);
                    me.setType(type);
                    me.setBindSw1(bindSw1);
                    me.setBindSw2(bindSw2);
                    me.setRegionId(regionId);
                    me.setDimmer(dimmer);
                    me.setGwDevId(gwDevId);
                    me.setDevCfg(devCfg);
                    me.setName(name);
                    db.saveOrUpdate(me);
                    Log.e(TAG, "This device Update,MAC:" + me.getMacAddr()
                            + "SerialNo:" + me.getDevId() + type);
                }
                Log.e(TAG, "This device registered,MAC:" + me.getMacAddr());
                return type;
            }
        } catch (DbException | NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
        return type;
    }

    public boolean del(String mac) {
        DbManager db = CommonUtil.MyDbUtils();
        try {
            DeviceDb me = db.selector(DeviceDb.class).where("macAddr", "=", mac).findFirst();
            if (me != null) {
                db.delete(me);
                return true;
            } else {
                Log.d(TAG, "deleteDev: Not found, mac:" + mac);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void reset() {
        DbManager db = CommonUtil.MyDbUtils();
        try {
            db.dropTable(DeviceDb.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public boolean bind(String mac, String bindMac, int group) {
        DbManager db = CommonUtil.MyDbUtils();
        try {
            DeviceDb me = db.selector(DeviceDb.class).where("macAddr", "=", mac).findFirst();
            if (me != null) {
                if (group >= 1)
                    me.setBindSw2(bindMac);
                else
                    me.setBindSw1(bindMac);

                db.saveOrUpdate(me);
                return true;
            } else {
                Log.d(TAG, "deleteDev: Not found, mac:" + mac);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
